/*
 * AddNormalArcCommand.java
 *
 * Created on 10 czerwiec 2007, 17:33
 */

package edu.uj.petri.commands;

import edu.uj.petri.view.ArcView;
import edu.uj.petri.view.NodeView;
import edu.uj.petri.view.NormalArcView;
import edu.uj.petri.view.View;
import java.util.ResourceBundle;

/**
 *
 * @author Paweł
 */
public class AddNormalArcCommand extends Command {
    
    private ArcView arcView;
    private View view;
    private NodeView end;
    private Memento beginMemento;
    private Memento endMemento;
    private Memento redoBeginMemento;
    private Memento redoEndMemento;
    private boolean firstTime=true;
    
     /** Creates a new instance of AddNormalArcCommand */
    public AddNormalArcCommand( ArcView arcView, View view, NodeView end ) {
        
        this.view = view;
        this.arcView = arcView;
        this.end = end;
    }

    public String toString() {
        
        return ResourceBundle.getBundle("edu/uj/petri/messages").getString("dodanie_krawedzi");
    }

    void execute() {
        if (firstTime) {
            beginMemento = arcView.getBegin().getNode().getMemento();
            endMemento = end.getNode().getMemento();
            arcView.setEnd( end ); // state change
            redoBeginMemento = arcView.getBegin().getNode().getMemento(); // we take the new state
            redoEndMemento = arcView.getEnd().getNode().getMemento();
            firstTime = false;
        } else {
            arcView.getBegin().getNode().applyMemento(redoBeginMemento);
            arcView.getEnd().getNode().applyMemento(redoEndMemento);
            view.addElement(arcView);
        }
        
        view.draw();
    }

    void undo() {
        arcView.getBegin().getNode().applyMemento(beginMemento);
        arcView.getEnd().getNode().applyMemento(endMemento);
        view.removeElement( arcView );
        view.draw();
    }

    boolean canBeCombined(Command c) {
        
        return false;
    }

    private static final long serialVersionUID = 575703450555500400L;
}
